const Model = require('../models/templatesModel')

const create = async (ctx) => {
  const { name, type, thumbnail, creator_id, industry_tag, style_tag, is_public } = ctx.request.body
  // console.log(name, type, thumbnail, creator_id, industry_tag, style_tag, is_public)
  try {
    const result = await Model.create(name, type, thumbnail, creator_id, industry_tag, style_tag, is_public)
    ctx.body = { message: '新增成功', id: result.insertId }
  } catch (error) {
    // console.error('新增失败:', error)
    ctx.status = 500
    ctx.body = { error: '新增失败' }
  }
}
const update = async (ctx) => {
  const { name, type, thumbnail, creator_id, industry_tag, style_tag, is_public, id } = ctx.request.body
  await Model.update(name, type, thumbnail, creator_id, industry_tag, style_tag, is_public, id)
  ctx.body = { success: true }
}
const removeById = async (ctx) => {
  const { id } = ctx.request.body
  await Model.removeById(id)
  ctx.body = { success: true }
}

const findById = async (ctx) => {
  const { roleId } = ctx.request.body
  ctx.body = { success: true, data: await Model.findById(roleId) }
}
const findAll = async (ctx) => {
  ctx.body = { success: true, data: await Model.findAll() }
}

module.exports = {
  create,
  update,
  removeById,
  findById,
  findAll,
}
